/*
 * jngc, Java utility classes by Nicholas Coltharp
 * Copyright (C) 2010 Nicholas Coltharp <ngc1@rice.edu>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package edu.rice.ngc1.util.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

/**
 * A {@code BufferedReader} subclass which provides a few handy
 * utility methods.
 */
public class ExtendedBufferedReader extends BufferedReader {

    /**
     * Constructs an {@code ExtendedBufferedReader} from the given
     * {@code Reader}
     * 
     * @param reader
     *            the {@code Reader} to read data from
     */
    public ExtendedBufferedReader(Reader reader) {
        super(reader);
    }

    /**
     * Constructs an {@code ExtendedBufferedReader}, wrapping the
     * provided stream in an {@link InputStreamReader} to read from.
     * 
     * @param stream
     *            the {@code InputStream} to use as a base
     */
    public ExtendedBufferedReader(InputStream stream) {
        this(new InputStreamReader(stream));
    }

    /**
     * Reads from this object into the provided {@code StringBuffer}
     * until EOF is hit.
     * 
     * @param buf
     *            the {@code StringBuffer} to read into
     * @throws IOException
     *             if a read error occurs
     */
    public void readAll(StringBuffer buf) throws IOException {
        String line;
        while ((line = readLine()) != null) {
            buf.append(line);
        }
    }

    /**
     * Reads from this object into the provided {@code StringBuilder}
     * until EOF is hit.
     * 
     * @param builder
     *            the {@code StringBuilder} to read into
     * @throws IOException
     *             if a read error occurs
     */
    public void readAll(StringBuilder builder) throws IOException {
        String line;
        while ((line = readLine()) != null) {
            builder.append(line);
        }
    }

    /**
     * Reads from this object until EOF is hit and returns all of the
     * read text as a {@code String}.
     * 
     * @return a {@code String} representing all of the data that was
     *         read
     * @throws IOException
     *             if a read error occurs
     */
    public String readAll() throws IOException {
        StringBuilder builder = new StringBuilder();
        readAll(builder);
        return builder.toString();
    }
}
